package com.adou.Controller;

import com.adou.common.controller.BaseController;
import com.adou.common.dto.*;
import com.adou.common.model.DefForm;
import com.adou.common.util.Const;
import com.adou.service.FormModelService;
import com.adou.service.FormSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;


/**
 * 表单及业务处理控制器(导入导出)
 */
@Controller
@Scope(value = "prototype")
@RequestMapping(value = "/formwork/")
public class FormWorkController extends BaseController {

    @Autowired
    private FormModelService modelservice;
    @Autowired
    private FormSearchService searchservice;

    /**
     * 获取表单HTML和对应字段值
     *
     * @param formId  表单ID
     * @param id 记录id  有流程时传流程id 无流程时传记录id
     * @param flowId  流程ID 流程实例
     */
    @RequestMapping(value = "getFormContent", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getFormContentByFlowId(Long flowId, Long formId,Long id) {
        try {
            Map<String, Object> map = modelservice.getFormContentByFlowId(flowId,formId,id);
            if ((int) map.get("res") > 0) {
                Map<String, Object> data=(Map<String, Object>)map.get("data");
                DefForm defForm=(DefForm)data.get("formInfo");
                DefFormBigOutDto defFormBigOutDto=new DefFormBigOutDto(defForm);
                data.put("formInfo", defFormBigOutDto);
                resultMap.put("data", data);
                resultMap.put("msg", Const.DATA_SUCCEED);
            } else {
                resultMap.put("result", Const.FORMCONTENT_ERROR_121);
                resultMap.put("msg", Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.FORMCONTENT_ERROR_122);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

    /**
     * 删除表单内容
     *
     * @param formId  表单ID
     * @param ids 记录id 多个以“,”分隔
     */
    @RequestMapping(value = "deleteFormContent", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteFormContentByIds(Long formId,String ids) {
        try {
            Map<String, Object> map = modelservice.deleteFormContentByIds(formId,ids);
            resultMap.putAll(map);
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DEL_ERROR_302);
            resultMap.put("msg", Const.DEL_FAIL);
        }
        return resultMap;
    }

    /**
     * 保存表单字段值
     *
     * @param formDataInDto addList 表单字段信息AddFieldDto数组（plugins 组件类型;type 字段类型;fieldname 字段名称;value 字段值;fieldflow 流程变量）;
     *                      flowId 流程id;formId 表单id;id 记录id  有流程时传流程id 无流程时传记录id
     */
    @RequestMapping(value = "setFormContent", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> setFormContentByFlowId(@RequestBody FormDataInDto formDataInDto) {
        try {
            int res = modelservice.setFormContentByFlowId(formDataInDto.getAddList(),
                    formDataInDto.getFlowId(),formDataInDto.getFormId(),formDataInDto.getId());
            if (res == 1) {
                resultMap.put("result", res);
                resultMap.put("msg", Const.SAVE_SUCCEED);
            } else {
                resultMap.put("result", Const.ADD_ERROR_201);
                resultMap.put("msg", Const.SAVE_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.ADD_ERROR_202);
            resultMap.put("msg", Const.SAVE_FAIL);
        }
        return resultMap;
    }


    /**
     * 获取查询数据(根据查询条件)
     *
     * @param conditionDataInDto formId 表单id;pageNum 页数;pageSize 页大小;conditionList 查询条件InConditionDto数组（id 条件id;findvalue1 查询字段1对应的查询值;
     *                           findvalue2 查询字段2对应的查询值 类型为区间时有效）;
     *
     */
    @RequestMapping(value = "getData", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getSearchData(@RequestBody ConditionDataInDto conditionDataInDto){
        try {
            Map<String, Object> map= searchservice.getSearchData(conditionDataInDto.getConditionList(),conditionDataInDto.getFormId(),
                    conditionDataInDto.getPageNum(),conditionDataInDto.getPageSize(),true);
            if ((int) map.get("res")==1) {
                resultMap.put("msg", Const.DATA_SUCCEED);
                resultMap.put("data",map.get("data"));
            } else {
                resultMap.put("result", Const.DATA_ERROR_111);
                resultMap.put("msg", Const.DATA_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.DATA_ERROR_112);
            resultMap.put("msg", Const.DATA_FAIL);
        }
        return resultMap;
    }

    /**
     * 导出表单数据
     *
     * @param conditionDataInDto formId 表单id;pageNum 页数(不传);pageSize 页大小(不传);conditionList 查询条件InConditionDto数组（id 条件id;findvalue1 查询字段1对应的查询值;
     *                           findvalue2 查询字段2对应的查询值 类型为区间时有效）;
     */
    @RequestMapping(value = "outData", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> outSearchData(@RequestBody ConditionDataInDto conditionDataInDto) {
        try {
            Map<String, Object>  map= searchservice.outSearchData(conditionDataInDto.getConditionList(),conditionDataInDto.getFormId());
            if ((int)map.get("res") == 1) {
                resultMap.put("msg", Const.OUT_SUCCEED);
                resultMap.put("data",map.get("data"));
            } else {
                resultMap.put("result", Const.OUT_ERROR_401);
                resultMap.put("msg", Const.OUT_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.OUT_ERROR_402);
            resultMap.put("msg", Const.OUT_FAIL);
        }
        return resultMap;
    }

    /**
     * 导入表单数据
     *
     * @param formId 表单id
     * @param file excel格式文件 字段顺序通过FormModelController的getFields接口函数获取
     */
    @RequestMapping(value = "inData", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> inDataToForm(Long formId,MultipartFile file) {
        try {
            Map<String, Object>  map= searchservice.inDataToForm(formId,file);
            if ((int)map.get("res") == 1) {
                resultMap.put("msg", Const.IN_SUCCEED);
                resultMap.put("data",map.get("data"));
            }else if ((int)map.get("res") == 211) {
                error((int)map.get("res"),"formId为空");
            }else if ((int)map.get("res") == 212) {
                error((int)map.get("res"),"表单未发布");
            } else {
                resultMap.put("result", Const.IN_ERROR_411);
                resultMap.put("msg", Const.IN_FAIL);
            }
        } catch (Exception e) {
            logger.error(e.toString(), e);
            resultMap.put("result", Const.IN_ERROR_412);
            resultMap.put("msg", Const.IN_FAIL);
        }
        return resultMap;
    }
}

